package cn.j229.bug.zhongxuan.hualian.service.impl;

import cn.j229.bug.zhongxuan.hualian.mapper.ManagerMapper;
import cn.j229.bug.zhongxuan.hualian.pojo.Manager;
import cn.j229.bug.zhongxuan.hualian.service.ManagerService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author smile_ZhongXuan
 * @Date 2020/10/23 9:50
 * @Version 1.0
 */
@Service("managerService")
public class ManagerServiceImpl implements ManagerService {

    @Resource
    ManagerMapper managerMapper = null;

    @Override
    public int addManager(Manager manager) {
        manager.setRegDate(new Date());
        manager.setState(false);
        return managerMapper.insertSelective(manager);
    }

    @Override
    public Manager loginManager(String phone) {
        Manager manager = new Manager();
        manager.setPhone(phone);
        return managerMapper.selectOne(manager);
    }

    @Override
    public int managerUpdateState(String phone,Boolean state) {
        Manager manager = new Manager();
        manager.setState(state);
        Example example = new Example(Manager.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone",phone);
        return managerMapper.updateByExampleSelective(manager,example);
    }

    @Override
    public int managerDelete(String phone) {
        Manager manager = new Manager();
        manager.setPhone(phone);
        return managerMapper.delete(manager);
    }

    @Override
    public List findManagerListByLike(Manager manager) {
        Example example = new Example(Manager.class);
        Example.Criteria criteria = example.createCriteria();
        if(manager.getName()!= null || !manager.getName().equals("")){
            criteria.orLike("name","%" + manager.getName() + "%");
        }
        if(manager.getPhone() != null || !manager.getPhone().equals("")){
            criteria.orLike("phone","%" + manager.getPhone() + "%");
        }
        return managerMapper.selectByExample(example);
    }
}
